//
//  CZQCoachMoveDetailVC.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/26.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQCoachMoveDetailVC.h"
#import "CZQCoachMoveDetailTCell.h"
#import "MyTableHead.h"

@interface CZQCoachMoveDetailVC ()
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

/**
 头
 */
@property(nonatomic,weak)CZQCoachMoveDetailHeadView *headView;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,assign)NSInteger pageSize;

@property(nonatomic,strong)NoDataView *noDataView;
@end

@implementation CZQCoachMoveDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"动态";
    
    self.view.backgroundColor = BackGroundColor;
    [self.tableView reloadData];
    
    [self getAttentList];
    // Do any additional setup after loading the view.
}


#pragma mark 下拉
-(void)loadStartData{
    
    self.index = 1;
    
    self.pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    self.index = self.index + 1;
    
    [self loadData];
}

#pragma mark - 获取自己动态数

-(void)loadData{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:[NSString stringWithFormat:@"%d",self.index] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",self.pageSize] forKey:@"rows"];
    
    [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"]] forKey:@"VoteMemberID"];;
    
    if (self.cycleModel) {
        
        [params setObject:self.cycleModel.MemberID forKey:@"MemberID"];
        
        [params setObject:self.cycleModel.UserType forKey:@"UserType"];
        
    }else
    {
        [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"]] forKey:@"MemberID"];
        
        [params setObject:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"]] forKey:@"UserType"];
    }
    
    
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    [HTTPTool GET:GET_Activity_List parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                      
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  if (self.index == 1) {
                      [self.dataArr removeAllObjects];
                  }
                  
                  
                  
                  
                  for (NSDictionary *resultDic in resultArray) {
                      
                      CZQCycleModel *cycle = [CZQCycleModel mj_objectWithKeyValues:resultDic];
                      
                      [self.dataArr addObject:cycle];
                      
                  }
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (self.dataArr.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView showInView:self.view];
                  }else
                  {
                      CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:0];
                      
                      self.headView.moveCountLable.text = [NSString stringWithFormat:@"%ld",self.dataArr.count];
                      
                      [self.headView.iconBtn sd_setImageWithURL:[Helper imageUrlStringToChinese:cycleModel.Pic imageType:nil] forState:UIControlStateNormal placeholderImage:PlaceHold];
                      
                      self.headView.nameLable.text = cycleModel.Name;
                      
                      self.headView.likeCountLable.text = cycleModel.VoteCount;

                  }
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

#pragma mark - 获取师傅关注列表
-(void)getAttentList
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"];
    
    
    
//    if (self.cycleModel) {
//        [params setValue:self.cycleModel.MemberID forKey:@"UserID"];
//        [params setValue :self.cycleModel.UserType forKey:@"UserType"];
//    }else
//    {
//        [params setObject:userId forKey:@"UserID"];
//
//        [params setObject:[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"] forKey:@"UserType"];
//    }
    
    
    [params setObject:userId forKey:@"MemberID"];
    
    [params setObject:[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"] forKey:@"UserType"];
    
    [HTTPTool GET:Get_Activity_ActivityFollowList parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@=%@",responseObject,userId);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  self.headView.attentionBtn.selected = NO;
                  
                  for (NSDictionary *dic in resultArray) {
                      
                      if ([dic[@"MemberID"] integerValue]  == [self.cycleModel.MemberID integerValue]) {
                          
                          self.headView.attentionBtn.selected = YES;
                          
                          return ;
                          
                      }
                      
                  }
                  
                  
                  
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
//        tableView.backgroundView = [UIColor clearColor];
        
        tableView.estimatedRowHeight = 150;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerClass:[CZQCoachMoveDetailTCell class] forCellReuseIdentifier:@"cell"];
        
        CZQCoachMoveDetailHeadView *headView = [[CZQCoachMoveDetailHeadView alloc] init];
        
        NSString *masrt = [[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"];
        
        if ([masrt integerValue] == 1) {
            headView.studyLable.hidden = YES;
            headView.studyCountLable.hidden = YES;
        }
        
        [headView.attentionBtn addTarget:self action:@selector(attent:)];
        
        
        
        
        [self.view addSubview:headView];
        
        self.headView = headView;
        [headView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(tableView);
            
            make.left.width.mas_equalTo(self.view);
        }];
        
        [headView setNeedsLayout];
        [headView layoutIfNeeded];
        
        tableView.tableHeaderView = headView;
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        footer.automaticallyHidden = YES;
        
        
        MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
        tableView.mj_header = mj_head;
        tableView.mj_footer = footer;
        [tableView.mj_header beginRefreshing];
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZQCoachMoveDetailTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    CZQCycleModel *cycleModel = [self.dataArr objectAtIndex:indexPath.row];
    
    if (cycleModel.PostTime) {
        NSString *time = [cycleModel.PostTime componentsSeparatedByString:@" "].firstObject;
        NSArray *arr = [time componentsSeparatedByString:@"-"];
        cell.timeMonthLable.text = [NSString stringWithFormat:@"%@\n%@月",arr.lastObject,arr[1]];
        
        cell.timeHoursLable.text = [NSString stringWithFormat:@"%@·发布了动态",[cycleModel.PostTime componentsSeparatedByString:@" "].lastObject];
    }
    
    cell.titleLable.text = cycleModel.Title;
    
    NSArray *picArr = nil;
    
    if (![cycleModel.Pics isEmptyString]) {
        picArr = [cycleModel.Pics componentsSeparatedByString:@","];
    }
    
    
    [cell.jggView JGGView:cell.jggView DataSource:picArr completeBlock:^(NSInteger index, NSArray *dataSource, NSIndexPath *indexpath) {
        
    }];
    
    cell.seeLable.text = [NSString stringWithFormat:@"%@人观看",cycleModel.ViewCount];
    
    [cell.talkBtn setTitle:cycleModel.CommentCount];
    
    [cell.goodBtn setTitle:cycleModel.ActivityCount];
    
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - 关注
-(void)attent:(UIButton *)sender
{
    
    
    NSString *url;
    
    NSString *tip;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    if (sender.selected == NO) {
        //关注
        url = Post_Activity_FollowOperate;
        
        tip = @"关注成功";
        
    }else
    {
        //取消关注
        url = Post_Activity_DelFollow;
        
        tip = @"取消关注成功";
        
    }
    
    [params setObject:[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"] forKey:@"MemberID"];
    [params setObject:self.cycleModel.MemberID forKey:@"ToMemberID"];
    [params setObject:[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"] forKey:@"UserType"];
    [params setObject:self.cycleModel.UserType forKey:@"ToUserType"];
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    
    
    [HTTPTool POSTRaw:url parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      sender.selected = !sender.selected;
                      
                      [MBProgressHUD showText:tip];
                      
                      [MBProgressHUD hideHUDForView:self.view];
                      
                      
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
